package put.cs.two.to.tsp.models;

import java.util.ArrayList;
import java.util.List;

public class TSPSolution {
    private List<Point> cycle = new ArrayList<Point>();

    public int getCycleLength() {
        int totalCycleLength = 0;
        Point currentPoint = null;
        for (Point p : cycle) {
            if (currentPoint != null) {
                int distance = NodeSet.getDistance(currentPoint, p);
                totalCycleLength += distance;
            }
            currentPoint = p;
        }
        totalCycleLength += NodeSet.getDistance(currentPoint, cycle.get(0));
        return totalCycleLength;
    }

    public void addPoint(Point point) {
        cycle.add(point);
    }

    public void insertPoint(Point point, int position) {
        cycle.add(position, point);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(getCycleLength() + " = ");
        for (Point point : cycle) {
            sb.append(point.getIndex() + " ");
        }
        return sb.toString();
    }
}
